package cn.topevery.garden.wapi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.topevery.garden.dal.logic.SectionLogic;
import cn.topevery.garden.dal.logic.SectionParkRelationLogic;
import cn.topevery.garden.entity.dto.SectionDTO;
import cn.topevery.garden.entity.po.Section;
import cn.topevery.garden.entity.po.SectionParkRelation;
import cn.topevery.garden.entity.query.SectionQuery;
import cn.topevery.garden.entity.vo.SectionVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.enums.GeometryTypeEnum;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Api(tags = "")
@RestController
@RequestMapping("/section")
public class SectionController extends BaseController {


    @Autowired
    SectionLogic sectionLogic;

    @Autowired
    SectionParkRelationLogic sectionParkRelationLogic;

    @Autowired
    Mapper mapper;

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<Section>> listPage(@RequestBody SectionQuery query) {
        return JsonResult.ok(sectionLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody SectionDTO dto) {
        return JsonResult.ok(true);//sectionLogic.saveOrUpdate(getUser().userId,dto));
    }

    @ApiOperation("获取标段key-value数据")
    @GetMapping("/getShortList")
    public JsonResult getShortList() {
        return JsonResult.ok(sectionLogic.getShortList());
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult getInfoById(@RequestParam Integer id) {
        Section entity = sectionLogic.getEntity(id);
        SectionVO vo= new SectionVO();
        BeanUtils.copyProperties(entity, vo);
        DbCsp query = new DbCsp();
        query.whereMap.add(new KeyValue<>("sectionId", id));
        List<SectionParkRelation> sectionParkRelationList = sectionParkRelationLogic.getSearch(query);
        vo.setParkIds(new ArrayList<>());
        sectionParkRelationList.forEach(f -> {
            vo.getParkIds().add(f.parkId);
        });
        return JsonResult.ok(vo);
    }


    @ApiOperation("获取概况数据")
    @GetMapping(value = "/getBasicInfo")
    public JsonResult<?> getBasicInfo(@RequestParam Integer id){
        Map<String,Object> res=sectionLogic.getBasicInfo(id);
        return JsonResult.ok(res);
    }

    @ApiOperation("保存数据")
    @PostMapping(value = "/save")
    public JsonResult<?> save(@RequestBody SectionDTO param) {
        try {
            SysUser user = UserRuntime.getSysUser();
            Section section = mapper.map(param,Section.class);
            if (CollUtil.isNotEmpty(param.getMapRangesPoints())) {
                section.setGeomWgs(GeometryHelper.getGeometry(GeometryHelper.getWellKnownText(param.getMapRangesPoints(), GeometryTypeEnum.PLANE)));
//                section.setGeoWkt(GeometryHelper.getWellKnownText(param.getMapRangesPoints(), GeometryTypeEnum.PLANE));
            }
            if (section.getId() == null) {
                section.sectionId = UUID.randomUUID().toString();
                section.state = 0;
            }

            if (section.id!=null&&section.id > 0) {
                sectionLogic.update(user.id.toString(), section);
            } else {
                sectionLogic.insertFillIdentity(user.id.toString(), section);
            }
            return JsonResult.ok(true);
        }
        catch (Exception ex) {
            System.out.print(ex.getStackTrace());
            return JsonResult.error("保存失败");
        }
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        sectionLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(SectionQuery query, HttpServletResponse response) {
        List<Section> list = sectionLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", Section.class, "", response);
    }

}
